Employee = function() {
	return {
		
		initDeptTree : function() {
			$("#companyDeptTree").tree({
				url        : '/pc/company/companyDeptTree.htm',
				checkbox   : false,
				onClick    : function(node) {
					$('#employeeGrid').datagrid({
						url         : '/pc/employee/all.htm', 
						queryParams : {
							'id'    : node.id
						},
						pageNumber  : 1,
						toolbar     : '#toolBar'
					}).load();
				},
				onDblClick : function(node) {
					if(node.state == 'closed') {
						$("#companyDeptTree").tree("expand", node.target);
					} else {
						$("#companyDeptTree").tree("collapse", node.target);
					}
				}
			});
		},
		
		initEmpGrid : function() {
			$('#employeeGrid').datagrid({  
		        fitColumns    : true,
		        striped       : true,
		        fit           : true,
		        rownumbers    : true,
		        singleSelect  : false,
		        autoRowHeight : true,
		        nowrap        : true,
		        pagination    : true,
		        pageSize      : 20,
		        columns       : [[ 
		            { field : 'employeeId',         title : 'id',                  checkbox : true,   width : 100 },  
		            { field : 'employeeCode',       title : '员工工号',             width    : 100 },  
		            { field : 'employeeName',       title : '姓名',                 width    : 100 },
		            { field : 'companyName',        title : '企业名称',             width    : 100 },
		            { field : 'departmentName',     title : '部门名称',             width    : 100 },
		            { field : 'headshipName',       title : '岗位名称',             width    : 100 },
		            { field : 'roleName',           title : '角色名称',             width    : 100 },
		            { field : 'mobile',             title : '手机号码',             width    : 100 },
		            { field : 'email',              title : '邮箱',                 width    : 100 }
		        ]],
		        toolbar: [{
		    		iconCls : 'add',
		    		text    : '添加',
		    		handler : function() {
						var row = $('#companyDeptTree').tree('getSelected');
						if(null == row || "" == row) {
							Ict.info("请选择企业或部门");
						} else {
							Ict.openWin("员工添加","400","300","/pc/employee/add.htm");
						}
		    		}
		    	},{
					text    : '修改',
					iconCls : 'edit',
					handler : function() {
						var row = $('#companyDeptTree').tree('getSelected');
						if(null == row || "" == row) {
							Ict.info("请选择企业或部门");
						} else {
							var rows = $('#employeeGrid').datagrid('getSelections');
							if (rows.length == 1) {
								Ict.openWin('员工修改',400,300,'/pc/employee/edit.htm?employeeId=' + rows[0].employeeId);
							} else {
								Ict.info('请选择一条记录进行修改', function() {
									return;
								});
							}
						}
					}
				},{
					text    : '删除',
					iconCls : 'remove',
					handler : function() {
						var row = $('#companyDeptTree').tree('getSelected');
						if(null == row || "" == row) {
							Ict.info("请选择企业或部门");
						} else {
							var rows = $('#employeeGrid').datagrid('getSelections');
							var num = rows.length;
							var employee = null;
							if (rows.length == 0) {
								Ict.info('请选择你要删除的员工', function() {
									return;
								});
							} else {
								Ict.confirm("确定要删除所选记录？",function(result) {
									if (result) {
										for ( var i = 0; i < num; i++) {
											if (null == employee|| i == 0) {
												employee = rows[i].employeeId;
											} else {
												employee = employee+ "," + rows[i].employeeId;
											}
										}
										$.ajax({
											url : "/pc/employee/delEmployee.htm",
											type : 'post',
											context : document.body,
											data : {"employee" : employee},
											dataType : 'json',
											success : function(data) {
												if (data == 1) {
													Ict.info("删除成功!",function() {
														$("#employeeGrid").datagrid("reload");});
												} else {
													Ict.error("删除失败！稍后重试。",function() {
														return;
													});
												}
											}
										});
									}
								});
							}
						}
					}
				}, {
		    		iconCls : 'reload',
		    		text    : '刷新',
		    		handler : function() {
		    			$('#employeeGrid').datagrid('reload');
		    		}
		    	}, {
					iconCls : 'add',
		    		text    : 'Excel导入',
		    		handler : function() {
						Ict.openWin("Excel导入", 500, 350, "/pc/employee/import.htm");
		    		}
				}]
		    });
		},
		
		//在toolbar上面添加搜索框
		addSearchBox : function(width) {
			$('.searchbox').css({'width': width+'px'});
			$('.searchbox-text').css({'width': eval(width-20)+'px'});
			$('.datagrid-toolbar').append($('.serarchbox'));
			$('.datagrid-toolbar').append($('#tbar'));
		},
		
		doSearch : function(value) {
			$('#employeeGrid').datagrid({
				url           : '/pc/employee/all.htm', 
				queryParams   : {
					'id'      : "",
					'keyword' : $.trim(value)
				},
				pageNumber    : 1,
				toolbar       : '#toolBar'
			}).load();
		}
	};
}();

$(function(){
	Employee.initDeptTree();
	Employee.initEmpGrid();
	Employee.addSearchBox(250);
});